const fs = require("fs");
const csv = require("csv-parser");

class JtlParser {

    constructor(jtlFilePath) {
        this.filePath = jtlFilePath;
        this.data = null;
    }

    async parse() {
        return new Promise((resolve, reject) => {
            this.data = {};
            fs.createReadStream(this.filePath).pipe(csv())
                .on("data", (row) => {
                    const label = row.label;
                    const elapsed = parseInt(row.elapsed);
                    const success = row.success === "true";
                    const timeStamp = parseInt(row.timeStamp);
                    const allThreads = parseInt(row.allThreads);
                    const sentBytes = parseInt(row.sentBytes);
                    const receivedBytes = parseInt(row.bytes);
                    const type = row.URL === "null" ? (/^[^\s-]+-[^\s-]+$/.test(row.label) ? "SUB_GROUP" : "GROUP") : "HTTP_REQUEST";

                    if (!this.data[label]) {
                        this.data[label] = {
                            responseTimes: [],
                            totalResponseTime: 0,
                            requestCount: 0,
                            successfulRequestCount: 0,
                            minTimeStamp: timeStamp,
                            maxTimeStamp: timeStamp,
                            maxAllThreads: allThreads,
                            failedRequestCount: 0,
                            minResponseTime: Infinity,
                            maxResponseTime: -Infinity,
                            totalSentBytes: 0,
                            totalReceivedBytes: 0,
                            type: type
                        };
                    }

                    this.data[label].responseTimes.push(elapsed);
                    this.data[label].totalResponseTime += elapsed;
                    this.data[label].requestCount++;
                    if (success) {
                        this.data[label].successfulRequestCount++;
                    } else {
                        this.data[label].failedRequestCount++;
                    }
                    this.data[label].minTimeStamp = Math.min(this.data[label].minTimeStamp, timeStamp);
                    this.data[label].maxTimeStamp = Math.max(this.data[label].maxTimeStamp, timeStamp);
                    this.data[label].maxAllThreads = Math.max(this.data[label].maxAllThreads, allThreads);
                    this.data[label].minResponseTime = Math.min(this.data[label].minResponseTime, elapsed);
                    this.data[label].maxResponseTime = Math.max(this.data[label].maxResponseTime, elapsed);
                    this.data[label].totalSentBytes += sentBytes;
                    this.data[label].totalReceivedBytes += receivedBytes;
                })
                .on("end", () => {
                    resolve(this.data);
                })
                .on("error", (error) => {
                    reject(error);
                });
        });

    }
}

module.exports = JtlParser;


